package com.james.customcamera.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.james.customcamera.R;
import com.james.customcamera.base.BaseActivity;
import com.james.customcamera.contract.RegisterContract;
import com.james.customcamera.presenter.RegisterPresenter;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RegisterActivity extends BaseActivity<RegisterPresenter> implements RegisterContract.RegisterView {


    @BindView(R.id.register_username)
    EditText registerUsernameEt;
    @BindView(R.id.register_nickname)
    EditText registerNicknameEt;
    @BindView(R.id.register_password)
    EditText registerPasswordEt;
    @BindView(R.id.register_password_again)
    EditText registerRepeatPasswordEt;
    @BindView(R.id.register_register)
    SuperButton registerRegisterBtn;
    @BindView(R.id.register_cancel)
    TextView registerCancelTv;
    @BindView(R.id.register_progress)
    ProgressBar registerProgressPb;

    @Override
    protected void initView() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_register;
    }

    @Override
    protected RegisterPresenter loadPresenter() {
        return new RegisterPresenter();
    }


    @Override
    protected void initData() {

    }

    @Override
    protected void otherViewClick(View view) {

    }


    @Override
    public void showLoading() {
        registerProgressPb.setVisibility(View.VISIBLE);
    }

    public void stopLoading() {
        registerProgressPb.setVisibility(View.GONE);
    }

    @Override
    public boolean isActive() {
        return false;
    }


    @OnClick({R.id.register_register, R.id.register_cancel})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.register_register:
                mPresenter.register(getUserName(), getNickName(), getPassword(), getRepeatPassword());
                break;
            case R.id.register_cancel:
                finish();
                break;
        }
    }


    @Override
    public String getUserName() {
        return registerUsernameEt.getText().toString().trim();
    }

    @Override
    public String getNickName() {
        return registerNicknameEt.getText().toString().trim();
    }

    @Override
    public String getPassword() {
        return registerPasswordEt.getText().toString().trim();
    }

    @Override
    public String getRepeatPassword() {
        return registerRepeatPasswordEt.getText().toString().trim();
    }


    @Override
    public void registerSuccess(String successMsg) {
        toast("注册成功");
        finish();
    }

    @Override
    public void registerFail(String failMsg) {
        toast(failMsg);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    public boolean checkNull() {
        boolean isNull = false;
        if (TextUtils.isEmpty(getUserName())) {
            registerUsernameEt.setError("账号不能为空");
            isNull = true;
        } else if (TextUtils.isEmpty(getPassword())) {
            registerPasswordEt.setError("密码不能为空");
            isNull = true;
        } else if (TextUtils.isEmpty(getNickName())) {
            registerNicknameEt.setError("昵称不能为空");
        } else if (TextUtils.isEmpty(getRepeatPassword())) {
            registerRepeatPasswordEt.setError("重复密码不能为空");
        }
        return isNull;
    }

    public boolean checkPassword() {
        boolean isDiffer = false;
        if (!TextUtils.equals(getPassword(), getRepeatPassword())) {
            toast("两次输入的密码不同");
            isDiffer = true;
        }
        return isDiffer;
    }
}
